package builder;

/**
 * terrain 地形
 */
class Terrain{
    Bunker b;
    Mountains m;
}

/**
 * bunker 地堡
 */
class Bunker{
    private int x;
    private int y;
    private int z;
    public Bunker(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

/**
 * mountains 山
 */
class Mountains{
    private int x;
    private int y;
    private int z;
    public Mountains(int x, int y, int z){
        this.x = x;
        this.y = y;
        this.z = z;
    }
}

interface TerrainBuilder{
    TerrainBuilder createBunker();
    TerrainBuilder createMountains();
    Terrain createBuilder();
}

class Terbuilder implements TerrainBuilder{
    private Terrain terrain = new Terrain();

    @Override
    public TerrainBuilder createBunker() {
        terrain.b = new Bunker(1,1,1);
        return this;
    }

    @Override
    public TerrainBuilder createMountains() {
        terrain.m = new Mountains(1,1,1);
        return this;
    }

    @Override
    public Terrain createBuilder() {
        return terrain;
    }
}

/**
 * 参数很多的情况使用静态工厂
 */
class Person{
    int x;
    int y;
    int z;
    int q;

    @Override
    public String toString() {
        return "Person{" +
                "x=" + x +
                ", y=" + y +
                ", z=" + z +
                ", q=" + q +
                '}';
    }

    public static class PersonBuilder{
        private Person p = new Person();

        PersonBuilder createX(int x){
            p.x = x;
            return this;
        }

        PersonBuilder createY(int y){
            p.y = y;
            return this;
        }
        Person builder(){
            return p;
        }
    }
}

public class Builder {
    public static void main(String[] args) {
        System.out.println("builder");

        TerrainBuilder terrainBuilder = new Terbuilder();
        Terrain t = terrainBuilder.createBunker().createMountains().createBuilder();

        System.out.println(t.m);

        Person p = new Person.PersonBuilder().createX(1).createY(1).builder();
        System.out.println(p);
    }
}
